package reporthandler

import (
	"github.com/gin-gonic/gin"
	"log"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// GetReportList 获取故障列表API
func GetReportList(ctx *gin.Context) {
	param := &request.ReportListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()

	// 查询结果
	data := dao.NewReportDao().GetReportList(param)
	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

// GetReportDetail 获取故障详情API
func GetReportDetail(ctx *gin.Context) {
	reportId := &struct {
		Id int `uri:"id" binding:"required,gt=0"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindUri(reportId)).Unwrap()

	log.Println("hahaha: ", reportId.Id)
	Response(ctx)("", CodeSuccess, dao.GetReportDetail(reportId.Id).Unwrap())(OK)

}

// HandleUpdateReport 更新故障状态
func HandleUpdateReport(ctx *gin.Context) {
	reportId := &struct {
		Id int `uri:"id" binding:"required,gt=0"`
		ReportDesc string `form:"rapairDesc" json:"rapairDesc"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(reportId)).Unwrap()

	log.Println("HandleUpdateReport: ", reportId.Id, reportId.ReportDesc)
	Response(ctx)("", CodeSuccess, dao.UpdateReport(reportId.Id, reportId.ReportDesc).Unwrap())(OK)

}
